home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / cl182.zip / CL.HPP < prev    next >
C/C++ Source or Header  |  1993-05-15  |  9KB  |  329 lines

  1. /*
  2.     cl.hpp -- Container Lite v 1.82
  3.     (C) Copyright 1993  John Webster Small
  4.     All rights reserved
  5. */
  6.  
  7.  
  8. #ifndef cl_hpp
  9. #define cl_hpp
  10.  
  11. #include <limits.h>    // UINT_MAX
  12. #include <stdarg.h>
  13. #include <iostream.h>
  14. #include <iomanip.h>
  15.  
  16. /*  stream delimiters/manipulators  */
  17.  
  18. extern char memberStreamDelimiter;
  19.  
  20. inline ostream& endm(ostream& os)
  21. { return os << memberStreamDelimiter << flush; }
  22.  
  23. inline istream& nextm(istream& is)
  24. { is.get(); return is; }
  25.  
  26.  
  27. /*  defined in cl.hpt and cl.hpf  */
  28. extern ostream& operator<<(ostream& os, char ** D);
  29. extern istream& operator>>(istream& is, char ** D);
  30.  
  31.  
  32. /*  cl pointer types  */
  33.  
  34. #define CLblank
  35. #define CLcmP(ID,ITEM)  \
  36.     int (*ID)(const ITEM * D1, const ITEM * D2)
  37. typedef CLcmP(voidCmP,void);
  38. #define CLcmPcast(ID,ITEM)  \
  39.     (CLcmP(CLblank,ITEM)) ID
  40. #define CLcmP0(ITEM)  CLcmPcast(0,ITEM)
  41. #define voidCmP0 ((voidCmP)0)
  42. #define CmPfnc CmP() const
  43.  
  44. #define CLapplY(ID,ITEM)  \
  45.     void (*ID)(ITEM * D, va_list args)
  46. typedef CLapplY(voidApplY,void);
  47. #define CLapplYcast(ID,ITEM)  \
  48.     (CLapplY(CLblank,ITEM)) ID
  49. #define CLapplY0(ITEM)  CLapplYcast(0,ITEM)
  50. #define voidApplY0 ((voidApplY)0)
  51.  
  52. typedef struct uniquenessGimmick { int x; }
  53.     * defaultConstructor;
  54. #define defaultConstruct    ((defaultConstructor)0)
  55.  
  56.  
  57. /*  cl constants  */
  58.  
  59. #define CL_MAXNODES    ((unsigned) \
  60.             (UINT_MAX/sizeof(void *)))
  61. #define CL_LIMIT    20U
  62. #define CL_DELTA    10U
  63. #define CL_NOTFOUND    CL_MAXNODES
  64.  
  65. #define CL_SORTED    0x01U
  66. #define CL_BIND_ONLY    0x00U
  67. #define CL_DASSIGN    0x02U
  68. #define CL_DREAD    0x04U
  69. #define CL_DREAD_DEL    0x08U
  70. #define CL_DNEW    0x10U
  71. #define CL_DNEWED    0x20U
  72. #define CL_DDELETE    0x40U
  73. #define CL_DSTORE    0x80U
  74. #define CL_ALL_FLAGS    0xFFU
  75. #define CL_DANDS    CL_DASSIGN|CL_DNEW|\
  76.             CL_DDELETE|CL_DSTORE
  77.  
  78.  
  79. class cl {
  80.  
  81. private:
  82.  
  83.     void init(unsigned flags = 0U,
  84.         unsigned maxNodes = 0U,
  85.         unsigned limit = 0U,
  86.         unsigned delta =0U);
  87.  
  88.  
  89. protected:
  90.  
  91.     unsigned lowLimit, lowThreshold, first;
  92.     void **  linkS;
  93.     unsigned limit,    delta,   nodes;
  94.     unsigned maxNodes, curNode, flags;
  95.     voidCmP  cmP;
  96.  
  97.     cl  (defaultConstructor)
  98.             { init(); }
  99.     void    assign(const cl& b);
  100.     void    destruct();
  101.     virtual voidCmP DcmP(voidCmP cmP)
  102.             { return cmP; }
  103.     virtual void * Dassign(void *, const void *)
  104.             { return (void *)0; }
  105.     virtual void * Dnew(const void *)
  106.             { return (void *)0; }
  107.     virtual void   Ddelete(void * D)
  108.             { delete D; }
  109.     virtual int    Dattach(void *)
  110.             { return 1; }
  111.     virtual void   Ddetach(void *)
  112.             {}
  113.     virtual    int    Dput(ostream&, void *)
  114.             { return 0; }
  115.     virtual    void * Dget(istream&) 
  116.             { return (void *)0; }
  117.     virtual int    put(ostream& os);
  118.     virtual int    get(istream& is);
  119.     int     load(const char * filename);
  120.     int     save(const char * filename);
  121.     void    vforEach(voidApplY B, va_list args);
  122.  
  123.  
  124. public:
  125.  
  126. /*  Constructors and destructor  */
  127.  
  128.     cl     (unsigned flags = CL_BIND_ONLY,
  129.         unsigned maxNodes = CL_MAXNODES,
  130.         unsigned limit = CL_LIMIT,
  131.         unsigned delta = CL_DELTA)
  132.         { init(flags,maxNodes,limit,delta); }
  133.     cl     (void ** argv, unsigned argc = 0U,
  134.         unsigned flags = CL_BIND_ONLY);
  135.     cl&     operator=(const cl& b)
  136.         { return *this; }
  137.     int     operator==(const cl& b) const;
  138.     int     operator> (const cl& b) const;
  139.     void ** vector(void ** argv = (void **)0,
  140.             unsigned argc = 0U) const;
  141.     virtual ~cl()  { destruct(); }
  142. /*
  143.     You must override this destructor in any
  144.     descendant that overrides Ddelete() or
  145.     Ddetach(), i.e.
  146.  
  147.     virtual ~DerivedFromCL()
  148.         { cl::destruct(); }
  149.  
  150.     This is because base destructors are called
  151.     after vfts are reset to their default values
  152.     for the base level.  This means that
  153.     cl::~cl() calls cl::Ddetach()
  154.     and perhaps cl::Ddelete() instead of
  155.     the intended DerivedFromCL::Ddetach()
  156.     or DerivedFromCL::Ddelete().
  157. */
  158.  
  159.  
  160. /*  Housekeeping Primitives  */
  161.  
  162.     unsigned Limit() const { return limit; }
  163.     unsigned setLimit(unsigned newLimit);
  164.     unsigned pack()  { return setLimit(nodes); }
  165.     unsigned Delta() const { return delta; }
  166.     unsigned setDelta(unsigned newDelta
  167.             = CL_DELTA);
  168.     unsigned Nodes() const { return nodes; }
  169.     unsigned MaxNodes() const { return maxNodes; }
  170.     unsigned setMaxNodes(unsigned newMaxNodes
  171.             = CL_MAXNODES);
  172.     unsigned vacancy() const
  173.             { return maxNodes - nodes; }
  174.     unsigned vacancyNonElastic() const
  175.             { return limit - nodes; }
  176.     unsigned Flags(unsigned flags = CL_ALL_FLAGS)
  177.             const
  178.             { return (this->flags & flags); }
  179.     unsigned setFlags(unsigned flags)
  180.             { return (this->flags |= flags); }
  181.     unsigned resetFlags(unsigned flags)
  182.             { return (this->flags &= ~flags); }
  183.     cl&  operator<<(cl& (*manipulator)
  184.             (cl&))
  185.             { return (manipulator?
  186.                 (*manipulator)(*this)
  187.                 : *this); }
  188.  
  189. /*  Elastic Array Primitives  */
  190.  
  191.     void *   atIns(unsigned n, void * D);
  192.     void *   atInsNew(unsigned n, const void * D);
  193.     void *   atRmv(unsigned n);
  194.     void     allRmv();
  195.     int      atDel(unsigned n);
  196.     void *   atDelAsg(unsigned n, void * D);
  197.     int      allDel();
  198.     void     allClr();
  199.     void *   atPut(unsigned n, void * D);
  200.     void *   atPutNew(unsigned n, const void * D);
  201.     void *   atPutAsg(unsigned n, const void * D);
  202.     void *   atGet(unsigned n) const;
  203.     void *   operator[](unsigned n) const
  204.             { return atGet(n); }
  205.     void *   atGetAsg(unsigned n, void * D);
  206.     void *   atXchg(unsigned n, void * D);
  207.     unsigned index(const void * D) const;
  208.     void     forEach(voidApplY B, ...)
  209.             { va_list args;
  210.             va_start(args,B);
  211.             vforEach(B,args);    
  212.             va_end(args); }
  213.  
  214.  
  215. /*  Stack - Deque - Queue Primitives  */
  216.  
  217.     void *   push(void * D)
  218.             { return atIns(0U,D); }
  219.     void *   pushNew(const void * D)
  220.             { return atInsNew(0U,D); }
  221.     void *   pop()  { return atRmv(0U); }
  222.     cl&      operator>>(void *& D)
  223.             { D = atRmv(0U);
  224.             return *this; }
  225.     int      popDel()  { return atDel(0U); }
  226.     void *   popDelAsg(void * D)
  227.             { return atDelAsg(0U,D); }
  228.     void *   top() const { return atGet(0U); }
  229.     void *   topAsg(void * D)
  230.             { return atGetAsg(0U,D); }
  231.     void *   insQ(void * D)
  232.             { return atIns(nodes,D); }
  233.     cl&      operator<<(void * D)
  234.             { atIns(nodes,D); return *this; }
  235.     void *   insQNew(const void * D)
  236.             { return atInsNew(nodes,D); }
  237.     void *   unQ()  { return atRmv(nodes-1); }
  238.     int      unQDel()  { return atDel(nodes-1); }
  239.     void *   unQDelAsg(void * D)
  240.             { return atDelAsg(nodes-1,D); }
  241.     void *   rear() const
  242.             { return atGet(nodes-1); }
  243.     void *   rearAsg(void * D)
  244.             { return atGetAsg(nodes-1,D); }
  245.  
  246. /*  List (single and double linked) Primitives  */
  247.  
  248.     unsigned CurNode() const;
  249.     int      setCurNode(unsigned n = CL_NOTFOUND);
  250.     void *   ins(void * D);
  251.     void *   insNew(const void * D);
  252.     void *   rmv();
  253.     int      del();
  254.     void *   delAsg(void * D);
  255.     void *   put(void * D)
  256.             { return atPut(curNode,D); }
  257.     void *   putNew(const void * D)
  258.             { return atPutNew(curNode,D); }
  259.     void *   putAsg(const void * D)
  260.             { return atPutAsg(curNode,D); }
  261.     void *   get() const { return atGet(curNode); }
  262.     void *   getAsg(void * D)
  263.             { return atGetAsg(curNode,D); }
  264.     void *   next();
  265.     void *   operator++()  { return next(); }
  266.     void *   nextAsg(void * D);
  267.     void *   prev();
  268.     void *   operator--()  { return prev(); }
  269.     void *   prevAsg(void * D);
  270.  
  271.  
  272. /* Priority Q, Set, Bag, Dictionary, Sort Primitives */
  273.  
  274.     unsigned Sorted() const
  275.             { return (flags & CL_SORTED); }
  276.     void     unSort()  { flags &= ~CL_SORTED; }
  277.     void     setCmP(voidCmP cmP = voidCmP0)
  278.             { this->cmP = DcmP(cmP);
  279.             flags &= ~CL_SORTED; }
  280.     voidCmP  CmP() const { return cmP; }
  281.     int      sort(voidCmP cmP = voidCmP0);
  282.     void *   insSort(void * D);
  283.     void *   insSortNew(const void * D);
  284.     void *   insUnique(void * D);
  285.     void *   insUniqueNew(const void * D);
  286.     void *   findFirst(const void * K);
  287.     void *   findNext(const void * K);
  288.     void *   findLast(const void * K);
  289.     void *   findPrev(const void * K);
  290.     unsigned findAll(const void * K);
  291.  
  292. };    /*  class cl  */
  293.  
  294.  
  295. inline ostream& operator<<(ostream& os, cl&)
  296. { return os; }
  297. inline istream& operator>>(istream& is, cl&)
  298. { return is; }
  299.  
  300.  
  301. typedef void (*GenericFnC)();
  302. #define GenericFnC0  ((GenericFnC)0)
  303.  
  304. class FunctionRegistry : cl  {
  305.  
  306. public:
  307.     FunctionRegistry() : cl(CL_DDELETE) {}
  308.     int regFnC(GenericFnC fnC, unsigned id);
  309.     unsigned fnC_2_ID(GenericFnC fnC);
  310.     GenericFnC ID_2_fnC(unsigned id);
  311.     void forget() { allDel(); }
  312.     ~FunctionRegistry() {}
  313. };
  314.  
  315. extern  FunctionRegistry fnCv;
  316.  
  317. #define ForgetFunctions() fnCv.forget()
  318. #define RegisterFunction(fnC,id)  \
  319.         fnCv.regFnC((GenericFnC)fnC,id)
  320. #define RegisterCmP(cmP,id)  \
  321.         RegisterFunction(cmP,id)
  322. #define fnC2ID(fnC) fnCv.fnC_2_ID((GenericFnC)fnC)
  323. #define ID2fnC(id)  fnCv.ID_2_fnC(id)
  324. #define cmP2ID(cmP) fnC2ID(cmP)
  325. #define ID2cmP(id) ((voidCmP)ID2fnC(id))
  326.  
  327.  
  328. #endif  /*  cl_hpp  */
  329.